package com.monitor.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.monitor.entity.ErpCustomerFinance;
import com.monitor.entity.ErpCustomerFinanceVice;
import com.monitor.service.ErpCustomerFinanceService;
import com.monitor.service.ErpCustomerFinanceViceService;
import com.monitor.util.DataUtil;
import com.monitor.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 客户财务表 前端控制器
 * </p>
 *
 * @author LX
 * @since 2018-09-06
 */
@RestController
@RequestMapping("/erpCustomerFinance")
@Api(description="客户财务")
public class ErpCustomerFinanceController {

    @Resource
    private ErpCustomerFinanceService financeService;

    @Resource
    private ErpCustomerFinanceViceService financeViceService;
    /**
     * Result<ErpCustomerFinance> result=new Result<ErpCustomerFinance>();
     try{

     List<Object> erpProjects=new ArrayList<Object>(JSONArray.parseArray(json,Object.class));
     for (int i = 0; i < erpProjects.size(); i++) {
     Object o =  erpProjects.get(i);

     }
     ErpCustomerFinance erpCustomerFinance = erpProjects.get(0);
     if (erpCustomerFinance.getErpCustomerFinanceVices()!=null){
     financeViceService.insertOrUpdateBatch(erpCustomerFinance.getErpCustomerFinanceVices());
     }
     financeService.insertOrUpdate(erpCustomerFinance);
     result.setCount(1);
     result.setData(erpCustomerFinance);
     }catch (Exception e){e.printStackTrace();}
     return result;
     */

    @RequestMapping("/inertOrUpdateCustomerFinance")
    @ApiOperation("添加Or修改客户财务信息// 并添加多条开户银行,")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")*/
    })
    public @ResponseBody
    Result<Boolean> inertOrUpdateCustomerFinance(ErpCustomerFinance erpCustomerFinance,@RequestParam(value="viceId",defaultValue="0")int viceId) {
        Result<Boolean> result=new Result<Boolean>();
        try{
           


//            ErpCustomerFinance erpCustomerFinance = erpProjects.get(0);
//            if (erpCustomerFinance.getErpCustomerFinanceVices()!=null){
//                financeViceService.insertOrUpdateBatch(erpCustomerFinance.getErpCustomerFinanceVices());
//            }
//            financeService.insertOrUpdate(erpCustomerFinance);
            erpCustomerFinance.setFinvDate(DataUtil.dateToStrLong(new Date()));
        	Boolean count=financeService.insertOrUpdate(erpCustomerFinance);
        	ErpCustomerFinanceVice erpCustomerFinanceVice = new ErpCustomerFinanceVice();
        	if(viceId!=0) {
        		erpCustomerFinanceVice.setId(viceId);
        	}
            erpCustomerFinanceVice.setCustomerId(erpCustomerFinance.getCustomerId());
            erpCustomerFinanceVice.setFinvBank(erpCustomerFinance.getFinvBank());
            erpCustomerFinanceVice.setFinvNumber(erpCustomerFinance.getFinvNumber());
            erpCustomerFinanceVice.setFinvType(erpCustomerFinance.getFinvType());
            erpCustomerFinanceVice.setFinvUser(erpCustomerFinance.getFinvUser());
            erpCustomerFinanceVice.setFinvDate(DataUtil.dateToStrLong(new Date()));
            erpCustomerFinanceVice.setFinanceId(erpCustomerFinance.getCustomerId());
            financeViceService.insertOrUpdate(erpCustomerFinanceVice);
            result.setCount(1);
            result.setData(count);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }
    @RequestMapping("/getcustFinanceByCustId")
    @ApiOperation("根据客户id客户财务信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result <List<Map<String,Object>>>  inertOrUpdateCustomerCar(Integer custId)  {
        Result<List<Map<String,Object>>> result=new  Result<List<Map<String,Object>>> ();
        try{
            ErpCustomerFinance customerCar=financeService.selectOne(new EntityWrapper<ErpCustomerFinance>().eq("customer_id",custId));
            List<ErpCustomerFinanceVice> customer_id = financeViceService.selectList(new EntityWrapper<ErpCustomerFinanceVice>().eq("customer_id", custId));
            List<Map<String,Object>> list=new ArrayList<>();
            for(ErpCustomerFinanceVice vice:customer_id){
                Map<String,Object> map=new HashMap<>();
                map.put("viceId",vice.getId());
                map.put("id",customerCar.getId());

                map.put("finvType",vice.getFinvType());
                map.put("finvNumber",vice.getFinvNumber());
                map.put("finvBank",vice.getFinvBank());
                map.put("finvUser",vice.getFinvUser());
                map.put("finvDate",vice.getFinvDate());
                map.put("custId",custId);
                map.put("finFrNo",customerCar.getFinFrNo());
                map.put("finDjNo",customerCar.getFinDjNo());
                map.put("finAddress",customerCar.getFinAddress());
                map.put("finType",customerCar.getFinType());
                list.add(map);
            }
            //            for (int i = 0; i < customer_id.size(); i++) {
//                ErpCustomerFinanceVice erpCustomerFinanceVice =  customer_id.get(i);
//                erpCustomerFinanceVice.setErpCustomerFinance(customerCar);
//            }
            if (null!=customer_id&&null!=customerCar)
            customerCar.setErpCustomerFinanceVices(customer_id);
            result.setCount(1);
            List<ErpCustomerFinance> erpCustomerFinances=new ArrayList<>();
            erpCustomerFinances.add(customerCar);
            result.setData(list);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }
    @RequestMapping("/removeCustomerFinance")
    @ApiOperation("删除客户财务信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "customerCarId", value = "客户财务ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Integer> removeCustomerFinance(Integer customerCarId) {
        Result<Integer> result=new Result<Integer>();
        try{
            result.setCount(1);
            result.setData(financeViceService.delete(new EntityWrapper<ErpCustomerFinanceVice>().eq("finance_id", customerCarId))?1:0);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getFinanceBycustIdPage")
    @ApiOperation("根据客户ID查询客户财务信息分页")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Page<ErpCustomerFinance>> getFinanceBycustIdPage(@RequestParam(value = "page", defaultValue = "1") int page,
                                                                @RequestParam(value="limit",defaultValue="10") int limit, Integer custId) {
        Result<Page<ErpCustomerFinance>> result=new Result<Page<ErpCustomerFinance>>();
        try{
            Page<ErpCustomerFinance> page1=new Page<ErpCustomerFinance>(page,limit);
            Page<ErpCustomerFinance> carPage=financeService.selectPage(page1,new EntityWrapper<ErpCustomerFinance>().eq("customer_id",custId));
            //Page<ErpCustomerCar> page=new Page<ErpCustomerCar>(1,2);
            result.setCount(carPage.getRecords().size());
            result.setData(carPage);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/updateFinance")
    @ApiOperation("修改财务信息")
    public Result<Boolean> updateFinance(ErpCustomerFinance finance){
    	Boolean count=financeService.updateById(finance);
    	return new Result<Boolean>(count, 1);
    }

}

